package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalOrders;
import com.fau.cm.entity.MedicalOrdersStorage;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 订单管理Action
 * @author kangyj
 * @datetime 2014年11月9日,下午5:32:49
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class OrdersAction extends PublicAction implements ModelDriven<MedicalOrders> {

	private static final long serialVersionUID = -8633854108483244322L;
	
	private MedicalOrders orders = new MedicalOrders();
	
	/**
	 * @desc 用户订单列表页初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月9日,下午5:38:38
	 */
	@Action(value = "orders_init", results = {@Result(location = "orders_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 查询用户订单信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月9日,下午5:36:30
	 */
	@Action(value = "orders_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		orders.setHospitalId(admin.getEnterpriseId());
		list = this.baseAllService.getOrdersManagerService().findOrdersByCid(orders);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 跳转到添加订单页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月20日,下午3:02:55
	 */
	@Action(value = "orders_add", results = {@Result(location = "orders_add.jsp")})
	public String add() {
		return SUCCESS;
	}
	
	/**
	 * @desc 保存订单
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月20日,下午3:03:50
	 */
	@Action(value = "orders_save")
	public String save() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String ordersId = this.baseAllService.getOrdersManagerService().saveOrders(orders, admin);
		map.put("success", true);
		map.put("ordersId", ordersId);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询媒体信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月20日,下午8:30:33
	 */
	@Action(value = "orders_find_media")
	public String findMediaList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getOrdersManagerService().findMediaList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询物流信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月20日,下午8:37:13
	 */
	@Action(value = "orders_find_logistics")
	public String findLogisticsList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getOrdersManagerService().findLogisticsList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 删除订单
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月24日,上午12:01:16
	 */
	@Action(value = "orders_del")
	public String del() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(orders.getId())) {
			this.baseAllService.getOrdersManagerService().delOrders(orders);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单修改
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月24日,上午9:24:45
	 */
	@Action(value = "orders_edit", results = {
			@Result(name = SUCCESS, location = "orders_edit.jsp"),
			@Result(name = "others_suc", location = "orders_edit1.jsp")})
	public String edit() {
		String userId = ((MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
		MedicalOrdersStorage order = this.baseAllService.getOrdersManagerService().findByOId(orders.getId());
		if(userId.equals(order.getCusSerId())) {
			return SUCCESS;
		} else {
			return "others_suc";
		}
	}
	
	/**
	 * @desc 订单修改保存
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月24日,上午9:24:45
	 */
	@Action(value = "orders_edit_save")
	public String editSave() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(StringUtils.isNotBlank(orders.getId())) {
			this.baseAllService.getOrdersManagerService().editOrders(orders, admin);
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单生效修改订单状态
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月25日,上午12:23:38
	 */
	@Action(value = "orders_edit_status")
	public String editOrdersStatus() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getOrdersManagerService().editOrdersStatus(orders);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询所有订单初始化 
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月18日,上午9:58:17
	 */
	@Action(value = "all_orders_inquiry_init", results = {@Result(name = SUCCESS, location = "all_orders_inquiry_init.jsp")})
	public String allOrdersInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 查询所有订单
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月18日,上午9:58:50
	 */
	@Action(value = "all_orders_inquiry_view")
	public String allOrdersView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		orders.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getOrdersManagerService().findAllStorageByPage(orders, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 中心订单查询初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月17日,下午11:16:51
	 */
	@Action(value = "center_orders_inquiry_init", results = {@Result(name = SUCCESS, location = "center_orders_inquiry_init.jsp")})
	public String centerOrdersInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 中心订单查询
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月17日,下午11:46:06
	 */
	@Action(value = "center_orders_inquiry_view")
	public String centerOrdersView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getOrdersManagerService().findCenterStorageByPage(orders, map, admin);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 组订单查询初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月17日,下午11:16:28
	 */
	@Action(value = "group_orders_inquiry_init", results = {@Result(name = SUCCESS, location = "group_orders_inquiry_init.jsp")})
	public String groupOrdersInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 组订单中心查询
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月17日,下午11:47:39
	 */
	@Action(value = "group_orders_inquiry_view")
	public String groupOrdersView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getOrdersManagerService().findGroupStorageByPage(orders, map, admin);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 我的订单查询初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月17日,下午11:16:36
	 */
	@Action(value = "my_orders_inquiry_init", results = {@Result(name = SUCCESS, location = "my_orders_inquiry_init.jsp")})
	public String myOrdersInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 我的订单查询
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月17日,下午11:44:33
	 */
	@Action(value = "my_orders_inquiry_view")
	public String myOrdersView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		orders.setCusSerId(admin.getId());
		pageBean = this.baseAllService.getOrdersManagerService().findMyStorageByPage(orders, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 修改订单总表
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月26日,下午8:55:17
	 */
	@Action(value = "orders_storage_edit")
	public String editOrdersStorage() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(orders.getId())) {
			this.baseAllService.getOrdersManagerService().editOrdersStorage(orders);
			map.put("success", true);          
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 再次提交审核
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月26日,下午4:25:02
	 */
	@Action(value = "orders_again_submit")
	public String againSubmit() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(orders.getId())) {
			this.baseAllService.getOrdersManagerService().againSubmit(orders);
			map.put("success", true);          
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 导出当前-所有
	 * @return null
	 * @author liy
	 * @datetime 2015年1月7日,上午11:17:52
	 */
	@Action(value = "order_export_cur_all")
	public String currentExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"所有订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportCurrentAll(orders.getId());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前-中心
	 * @return null
	 * @author liy
	 * @datetime 2015年1月7日,下午2:39:54
	 */
	@Action(value = "order_export_cur_center")
	public String currentExportCenter() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportCurrentAll(orders.getId());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前-本组
	 * @return null
	 * @author liy
	 * @datetime 2015年1月7日,下午2:58:13
	 */
	@Action(value = "order_export_cur_group")
	public String currentExportGroup() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"本组订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportCurrentAll(orders.getId());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前-我的
	 * @return null
	 * @author liy
	 * @datetime 2015年1月7日,下午3:11:51
	 */
	@Action(value = "order_export_cur_my")
	public String currentExportMy() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"我的订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportCurrentAll(orders.getId());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出全部-所有
	 * @return null
	 * @author liy
	 * @datetime 2015年1月7日,上午11:45:05
	 */
	@Action(value = "order_export_all_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"所有订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().allExportCurrentAll(orders);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出全部-中心
	 * @return null
	 * @author liy
	 * @datetime 2015年1月7日,下午2:45:43
	 */
	@Action(value = "order_export_all_center")
	public String allExportCenter() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getOrdersManagerService().allExportCenter(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出全部-本组
	 * @return null
	 * @author liy
	 * @datetime 2015年1月7日,下午3:00:56
	 */
	@Action(value = "order_export_all_group")
	public String allExportGroup() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"本组订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getOrdersManagerService().allExportGroup(orders, admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出全部-我的
	 * @return null
	 * @author liy
	 * @datetime 2015年1月7日,下午3:14:56
	 */
	@Action(value = "order_export_all_my")
	public String allExportMy() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"我的订单查询表.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			orders.setCusSerId(admin.getId());
			Workbook workbook = this.baseAllService.getOrdersManagerService().allExportMy(orders);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 查询客户原来下的订单
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月14日,下午5:11:06
	 */
	@Action(value = "customer_history_orders_init", results = {@Result(name = SUCCESS, location = "customer_history_orders_init.jsp")})
	public String historyOrdersInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 打开订单页面查询客户原来下的订单
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月14日,下午5:26:20
	 */
	@Action(value = "customer_history_orders_view")
	public String historyOrdersView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getOrdersManagerService().findHistoryList(orders.getCustomerId(), orders.getId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 计算折扣
	 * @return
	 * @author kangyj
	 * @datetime 2015年3月14日,下午12:14:43
	 */
	@Action(value = "orders_edit_calc_discount")
	public String calcDiscount() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getOrdersManagerService().calcDiscount(orders);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalOrders getModel() {
		return orders;
	}
	
	/**
	 * @desc 查询系统信息
	 * @return
	 * @author zhaoey
	 * @datetime 2015年4月11日,下午1:51:13
	 */
	@Action(value = "orders_find_system")
	public String findSystemList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getOrdersManagerService().findSystemList(orders.getKeyType());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询订单状态
	 * @return
	 * @author zhaoey
	 * @datetime 2015年4月11日,下午1:51:13
	 */
	@Action(value = "orders_find_order_status")
	public String findOrderStatusList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getOrdersManagerService().findOrderStatusList(orders.getKeyType());
		formatJSON(response, list);
		return null;
	}

}
